<p:config xmlns:p="http://www.orbeon.com/oxf/pipeline" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:oxf="http://www.orbeon.com/oxf/processors" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xxforms="http://orbeon.org/oxf/xml/xforms" xmlns:xdb="http://orbeon.org/oxf/xml/xmldb" xmlns:xu="http://www.xmldb.org/xupdate">

	<p:param type="input" name="instance"/>
	<p:param type="output" name="data"/>
	<p:param type="output" name="instance"/>

	<!--Get Session-->
	<p:processor name="oxf:pipeline">
		<p:input name="config" href="../session.xpl"/>
		<p:output name="data" id="session"/>
	</p:processor>

	<!--GROUPS -->
	<p:choose href="#instance">
		<p:when test="not(exists(/groups))">
			<p:processor name="oxf:identity">
				<p:input name="data" href="#session#xpointer(/session/groups)"/>
				<p:output name="data" id="groups"/>
			</p:processor>

		</p:when>
		<p:otherwise>
			<p:processor name="oxf:xslt">
				<p:input name="data" href="#session"/>
				<p:input name="instance" href="#instance"/>
				<p:output name="data" id="new_session"/>
				<p:input name="config">
					<xsl:stylesheet version="2.0">
						<xsl:template match="/">
							<xsl:copy>
								<xsl:apply-templates/>
							</xsl:copy>
						</xsl:template>
						<xsl:template match="groups">
							<xsl:copy-of select="doc('input:instance')"/>
						</xsl:template>
						<xsl:template match="@*|node()" priority="-2">
							<xsl:copy>
								<xsl:apply-templates select="@*|node()"/>
							</xsl:copy>
						</xsl:template>
					</xsl:stylesheet>
				</p:input>
			</p:processor>
			<p:processor name="oxf:scope-serializer">
				<p:input name="config">
					<config>
						<key>session</key>
						<scope>session</scope>
						<session-scope>application</session-scope>
					</config>
				</p:input>
				<p:input name="data" href="#new_session"/>
			</p:processor>

			<p:processor name="oxf:identity">
				<p:input name="data" href="#instance"/>
				<p:output name="data" id="groups"/>
			</p:processor>

		</p:otherwise>
	</p:choose>
	<p:processor name="oxf:identity">
		<p:input name="data" href="#groups"/>
		<p:output name="data" ref="instance"/>
	</p:processor>
	<!--END GROUPS -->


	<!--read global parameters-->
	<p:processor name="oxf:identity">
		<p:input name="data" href="/config/config.xml"/>
		<p:output name="data" id="global_pref"/>
	</p:processor>

	<!--load translation-->
	<p:processor name="oxf:pipeline">
		<p:input name="data" href="#session#xpointer(/session/lang)"/>
		<p:input name="config" href="/apps/engine/utils/XMLtranslation.xpl"/>
		<p:output name="data" id="translation"/>
	</p:processor>

	<!--Get Cop1-->
	<p:processor name="oxf:xmldb-query">
		<p:input name="datasource" href="/config/datasource.xml"/>
		<p:input name="query">
			<xdb:query collection="/db/portal">
				<cop1>{/cops/cop[@id='cop1']/text()}</cop1>
			</xdb:query>
		</p:input>
		<p:output name="data" id="cop"/>
	</p:processor>

	<!--Create data-->
	<p:processor name="oxf:unsafe-xslt">
		<p:input name="data" href="#session"/>
		<p:input name="cop" href="#cop"/>
		<p:input name="config">
			<data xsl:version="2.0">
				<username>
					<xsl:value-of select="/session/username"/>
				</username>
				<copName>
					<xsl:value-of select="doc('input:cop')"/>
				</copName>
				<serviceBrowserUri use="false"/>
				<widgetManagement>
					<xsl:choose>
						<xsl:when test="/session/level = 2">
							<xsl:text>true</xsl:text>
						</xsl:when>
						<xsl:otherwise>
							<xsl:text>false</xsl:text>
						</xsl:otherwise>
					</xsl:choose>
				</widgetManagement>
			</data>
		</p:input>
		<p:output name="data" id="datas"/>
	</p:processor>
	<p:processor name="oxf:pipeline">
		<p:input name="config" href="/apps/engine/utils/session_rewriter.xpl"/>
	</p:processor>

	<p:processor name="oxf:identity">
		<p:input name="data" href="aggregate('data', #datas#xpointer(data/*), #global_pref#xpointer(/globalpref/appname), #translation)"/>
		<p:output name="data" ref="data"/>
	</p:processor>
</p:config>